package com.iddactylogram.common.utils;

import com.iddactylogram.common.domain.MessageObject;

import java.nio.ByteBuffer;
import java.util.concurrent.ConcurrentMap;

public final class MessageParseUtils {
    private MessageParseUtils() {
    }



    public static MessageObject parse(ByteBuffer buffer) {
        buffer.flip();
        byte[] data = new byte[buffer.limit()];
        buffer.get(data);

        byte cmd = data[0];
        byte len = data[1];
        String message = new String(data, 2, len);
        return new MessageObject(cmd, message);
    }

    public static ByteBuffer createWriteBuf(byte[] writeBytes, byte cmd) {
        byte[] data = new byte[128];
        ByteBuffer buffer = ByteBuffer.allocate(128);
        data[0] = cmd;
        data[1] = (byte) writeBytes.length;
        System.arraycopy(writeBytes, 0, data, 2, writeBytes.length);
        buffer.put(data);
        return buffer;
    }
}
